﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Agen.ServiceAgencyWPF;
using Agen.Models;

namespace Agen.Views
{
    public partial class AddCustomer : Page
    {
        string STATE_CONTROL;


        public AddCustomer()
        {
            InitializeComponent();
            STATE_CONTROL = "";
            customer_radio.IsChecked = true;
            ApplicationManager.client.AddCustomerFromUserCompleted += new EventHandler<AddCustomerFromUserCompletedEventArgs>(client_AddCustomerFromUserCompleted);
            ApplicationManager.client.AddCustomerFromForumCompleted += new EventHandler<AddCustomerFromForumCompletedEventArgs>(client_AddCustomerFromForumCompleted);
        }

        void client_AddCustomerFromForumCompleted(object sender, AddCustomerFromForumCompletedEventArgs e)
        {
            if (e.Result)
            {
                reinitDatacontext();
            }
            else {
                MessageBox.Show("The forum hasn't been added !");            
            }
        }

        void client_AddCustomerFromUserCompleted(object sender, AddCustomerFromUserCompletedEventArgs e)
        {
            if (e.Result)
            {
                reinitDatacontext();
            }
            else {
                MessageBox.Show("The client hasn't been created !");         
            }
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
             DataContext = new AddCustomerVO();
        }


        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            reinitDatacontext();
            if (STATE_CONTROL == "forum" || STATE_CONTROL == "")
            {
                STATE_CONTROL = "customer";
                customerforum_contentcontrol.IsEnabled = false;
                newcustomer_contentcontrol.IsEnabled = true;
            }
            else {
                STATE_CONTROL = "forum";
                customerforum_contentcontrol.IsEnabled = true;
                newcustomer_contentcontrol.IsEnabled = false;
            }
        }

        public void enableControlsStackPanel(StackPanel panel) { 
            foreach(Control control in panel.Children){
                control.IsEnabled = true;
            }
        }

        public void disableControlsStackPanel(StackPanel panel)
        {
            foreach (Control control in panel.Children)
            {
                control.IsEnabled = false;
            }
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void add_btn_Click(object sender, RoutedEventArgs e)
        {    
            AddCustomerVO vo = DataContext as AddCustomerVO;
            if (STATE_CONTROL == "customer")
            { 
                if(vo.passwordRetype == vo.customer.Person.Password){
                    ApplicationManager.client.AddCustomerFromUserAsync(vo.customer);
                }else{
                    MessageBox.Show("The passwords are not the same !");
                }
            }
            else
            {
                ApplicationManager.client.AddCustomerFromForumAsync(vo.emailForum, ApplicationManager.CURRENT_EMPLOYEE);
            }
            
        }

        private void cancel_btn_Click(object sender, RoutedEventArgs e)
        {
            ApplicationManager.FrameAction(this, ApplicationManager.PREVIOUS_HANDLE, new Object());
        }

        private void activateCode_Unchecked(object sender, RoutedEventArgs e)
        {
            AddCustomerVO vo = DataContext as AddCustomerVO;
            vo.customer.dateCode = null;
            DataContext = vo;
        }
        private void reinitDatacontext() {
            DataContext = null;
            DataContext = new AddCustomerVO();
        }

    }

    public class AddCustomerVO
    {
        public AddCustomerVO()
        {
            customer = new CustomerDC();
            customer.Person = new PersonDC();
            customer.Commercial = ApplicationManager.CURRENT_EMPLOYEE;
        }

        public CustomerDC customer
        {
            get;
            set;
        }

        public string emailForum
        {
            get;
            set;
        }

        public string passwordRetype
        {
            get;
            set;
        }

    }
}
